package mine;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;

/**
 * 
 * @author DQ
 * 软件2班 邓强
 */
public class MainGameUI extends JFrame {
	private static final long serialVersionUID = 1L;
	protected ClearMine clearMine;
	protected int rows;
	protected int cols;		
	protected int mineCount;
	protected int[][] mineMap;
	protected int size = 20;
	protected boolean isBomb = false;

	private Image[] images = {
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/0.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/1.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/2.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/3.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/4.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/5.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/6.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/7.jpg")),
			Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/8.jpg")),
	};

	private Image mineImage = Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/bomb.jpg"));
	private Image bombImage = Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/bomb0.jpg"));
	private Image flagImage = Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/flag.jpg"));
	private Image questionImage = Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/flag2.jpg"));
	private Image initImage = Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/s.jpg"));

	public MainGameUI() {
		clearMine = new ClearMine();		
	}

	public void open() {
		creatContent();
		reset(2);
	}

	public void reset(int level) {
		clearMine.setLevel(level);
		rows = clearMine.getRows();
		cols = clearMine.getCols();
		mineCount = clearMine.getMineCount();
		mineMap = clearMine.getMineMap();

		this.setSize(cols * size + 8, rows * size + 65);
		this.setLocationRelativeTo(null);
		repaint();
	}

	private void creatContent() {
		this.setTitle("扫雷");
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(MainGameUI.class.getResource("/images/tetris.jpg")));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);

		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);

		JMenu menu = new JMenu("设置");
		menu.setFont(new Font("微软雅黑", Font.PLAIN, 12));

		menuBar.add(menu);

		ButtonGroup buttonGroup = new ButtonGroup();
		
		
		JRadioButtonMenuItem menuItem_1 = new JRadioButtonMenuItem("初级(雷数10)");
		menuItem_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));

		menu.add(menuItem_1);
		
		
		JRadioButtonMenuItem menuItem_2 = new JRadioButtonMenuItem("中级(雷数40)");
		menuItem_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));

		menu.add(menuItem_2);
		
		
		menuItem_2.setSelected(true);

		JRadioButtonMenuItem menuItem_3 = new JRadioButtonMenuItem("高级(雷数99)");
		menuItem_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		
		menu.add(menuItem_3);

		buttonGroup.add(menuItem_1);
		buttonGroup.add(menuItem_2);
		buttonGroup.add(menuItem_3);

		
		menuItem_1.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {
					reset(1);
				}

			}
		});

		menuItem_2.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {
					reset(2);
				}

			}
		});

		menuItem_3.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {
					reset(3);
				}

			}
		});

		this.setVisible(true);	
	}

	public void paint(Graphics g) {
		super.paint(g);

		if (isBomb) {
			int j = 0;
			for(int i = 0; i < rows; ++ i) {
				for(j = 0; j < cols; ++ j) {
					if ( (mineMap[i][j] & 0b00000001) == 1) {
						if ( (mineMap[i][j] & 0b00000110) == 0b00000100 ) {
							g.drawImage(mineImage, j * size + 4, i * size + 50, size, size, this);
						} else {
							g.drawImage(bombImage, j * size + 4, i * size + 50, size, size, this);
						}
					} else if( ((mineMap[i][j] >>> 3 ) == 0)) {
						g.drawImage(images[0], j * size + 4, i * size + 50, size, size, this);
					} else {
						g.drawImage(images[mineMap[i][j] >>> 3], j * size + 4, i * size + 50, size, size, this);
					}
				}
			}
		} else {
			int j = 0;
			for (int i = 0; i < rows; ++i) {
				for (j = 0; j < cols; ++j) {
					if ((mineMap[i][j] & 0b00000110) == 0) {
						g.drawImage(initImage, j * size + 4, i * size + 50, size, size, this);
					}else if( (mineMap[i][j] & 0b00000110) == 0b00000010 ) {
						if ((mineMap[i][j] & 0b00000001) == 1 ) {
							g.drawImage(mineImage, j * size + 4, i * size + 50, size, size, this);
						} else if( ((mineMap[i][j] >>> 3 ) == 0)) {
							g.drawImage(images[0], j * size + 4, i * size + 50, size, size, this);
						} else {
							g.drawImage(images[mineMap[i][j] >>> 3], j * size + 4, i * size + 50, size, size, this);
						}
					} else if( (mineMap[i][j] & 0b00000110) == 0b000000100) {
						g.drawImage(flagImage, j * size + 4, i * size + 50, size, size, this);
					} else if( (mineMap[i][j] & 0b00000110) == 0b000000110) {
						g.drawImage(questionImage, j * size + 4, i * size + 50, size, size, this);
					}
				}
			}
		}

		g.setColor(Color.gray);
		for(int i = 0; i <= rows; ++i) {
			g.drawLine(4, i * size + 50, cols * size + 4, i * size + 50);
		}

		for(int i = 0; i < cols; ++i) {
			g.drawLine(i * size + 4, 50, i * size + 4, rows * size + 50);
		}
	}
}	

